package com.ruoyi.system.service.impl;


import com.ruoyi.system.service.SmallToolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;
import java.util.Random;


/**
 * @author Yzy
 * Date: 2023/12/25
 * Time: 16:24
 */

@Service
@Slf4j
public class SmallToolServiceImpl implements SmallToolService {

    @Autowired
    private RestTemplate restTemplate;

    //万年历
    private final String HOLIDAY_SINGLE_API = "http://www.mxnzp.com/api/holiday/single/";
    //天气
    private final String WEATHER_API = "https://www.mxnzp.com/api/weather/current/";
    //讲笑话
    private final String JOKES_LIST_API = "http://www.mxnzp.com/api/jokes/list";
    //
    private final String HOLIDAY_LIST_API = "http://www.mxnzp.com/api/holiday/recent/list";
    //获取当日和未来的天气
    private final String WEATHER_FORECAST_API = "http://www.mxnzp.com/api/weather/forecast/";
    private final String NEWS_TYPES_API = "http://www.mxnzp.com/api/news/types/v2";


    //万年历小工具
    @Override
    public String getCalendar(String datatime) {
        String url = HOLIDAY_SINGLE_API + datatime;
        String ignoreHoliday = "false";
        String app_id = "bkqratpotpklklps";
        String app_secret = "3OHyIf6Z0sDnWtUF3ctmOPQR8al0sdEr";
        HttpHeaders headers = new HttpHeaders();
        headers.set("ignoreHoliday", ignoreHoliday);
        headers.set("app_id", app_id);
        headers.set("app_secret", app_secret);
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        HttpEntity request = new HttpEntity(headers);
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                request,
                String.class
        );
        log.info("=========================请求fw名录接口===============================");
        log.info(url);
        log.info(String.valueOf(response.getBody()));
        log.info("========================请求返回结果 ↑↑↑===============================");
        String ans = response.getBody();
        return ans;
    }

    @Override
    public String getWeather(String cityName) {
        String app_id = "bkqratpotpklklps";
        String app_secret = "3OHyIf6Z0sDnWtUF3ctmOPQR8al0sdEr";
        String url = WEATHER_API + cityName +"?app_id="+app_id+"&app_secret="+app_secret;
        HttpHeaders headers = new HttpHeaders();
//        headers.set("app_id", app_id);
//        headers.set("app_secret", app_secret);
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(5000);
        requestFactory.setReadTimeout(10000);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        try {
            HttpEntity request = new HttpEntity(headers);
            ResponseEntity<String> response = restTemplate.exchange(
                    url,
                    HttpMethod.GET,
                    request,
                    String.class
            );
            log.info("=========================请求fw名录接口===============================");
            log.info(url);
            log.info(String.valueOf(response.getBody()));
            log.info("========================请求返回结果 ↑↑↑===============================");
            String ans = response.getBody();
            return ans;
        } catch (RuntimeException e) {
            System.out.println("单个请求设置超时时间，请求超时");
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public String getRecentWeather(String cityName) {
        String url = WEATHER_FORECAST_API + cityName;
        String app_id = "bkqratpotpklklps";
        String app_secret = "3OHyIf6Z0sDnWtUF3ctmOPQR8al0sdEr";
        HttpHeaders headers = new HttpHeaders();
        headers.set("app_id", app_id);
        headers.set("app_secret", app_secret);
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        HttpEntity request = new HttpEntity(headers);
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                request,
                String.class
        );
        log.info("=========================请求fw名录接口===============================");
        log.info(url);
        log.info(String.valueOf(response.getBody()));
        log.info("========================请求返回结果 ↑↑↑===============================");
        String ans = response.getBody();
        return ans;
    }

    @Override
    public String getJokes() {
        Random rand = new Random();
        String page = String.valueOf(rand.nextInt(50) + 1);
        String url = JOKES_LIST_API+"?page="+page;


        String app_id = "bkqratpotpklklps";
        String app_secret = "3OHyIf6Z0sDnWtUF3ctmOPQR8al0sdEr";
        HttpHeaders headers = new HttpHeaders();
//        headers.set("page", page);
        headers.set("app_id", app_id);
        headers.set("app_secret", app_secret);
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        HttpEntity request = new HttpEntity(headers);
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                request,
                String.class
        );
        log.info("=========================请求fw名录接口===============================");
        log.info(url);
        log.info(String.valueOf(response.getBody()));
        log.info("========================请求返回结果 ↑↑↑===============================");
        String ans = response.getBody();
        return ans;
    }
}
